--[[
	Copyright (C) Udorn (Blackhand)
--]]

local viewType = "Text"
local version = 1
local YakaGui = LibStub("YakaGui", true)
local info = YakaGui:GetViewInfo(viewType) 
if (info and info.version >= version) then 
	return 
end

yaka = yaka or {}
yaka.Text = {}
yaka.Text.prototype = {}
yaka.Text.metatable = {__index = yaka.Text.prototype}
setmetatable(yaka.Text.prototype, {__index = yaka.View.prototype})

yaka.Text.info = {viewType = viewType, version = version}
yaka.Text.GetViewInfo = function()
	return yaka.Text.info
end

local log = vendor.Debug:new("Text")

local function _OnClick(this, button)
	local self = this.view
	local info = {arg = self.cfg.arg, view = self, button = button}
	if (self.cfg.onClick) then
		self.cfg.onClick(info)
	end
end

local function _InitFrame(self)
	frame = CreateFrame("Button", nil, UIParent)
	frame.view = self
	
	frame:SetPoint("CENTER")
	if (self.cfg.onClick) then
		frame:RegisterForClicks("LeftButtonUp", "RightButtonUp")
		frame:SetScript("OnClick", _OnClick)
	end
	
	local text = frame:CreateFontString(nil, "ARTWORK", self.cfg.font)
    text:SetAllPoints(frame)
    text:SetJustifyH("LEFT")
    text:SetJustifyV("MIDDLE")
    
	self.frame = frame
	self.text = text
end

--[[ 
	Creates a new instance.
--]]
function yaka.Text:new(cfgIn)
	local instance = setmetatable({}, self.metatable)
	instance:InitCfg(cfgIn)
	instance:InitFrame()
	return instance
end

function yaka.Text.prototype:InitCfg(cfgIn)
	yaka.View.prototype.InitCfg(self, cfgIn)
	local cfg = self.cfg
	cfg.text = cfgIn.text or ""
	cfg.onClick = cfgIn.onClick
	cfg.getValue = cfgIn.getValue
	cfg.font = cfgIn.font or "GameFontHighlight"
	cfg.arg = cfgIn.arg
end

function yaka.Text.prototype:InitFrame()
	local cfg = self.cfg
	_InitFrame(self)
	yaka.View.prototype.InitFrame(self)
	if (cfg.getValue) then
		self:SetText(cfg.getValue({view = self, arg = cfg.arg}))
	else
		self:SetText(cfg.text)
	end
end


function yaka.Text.prototype:SetText(text)
	self.text:SetText(text or "")
end

function yaka.Text.prototype:GetMeasuredHeight(width)
	--log:Debug("GetMeasuredHeight width [%s]", width)
	local height = yaka.View.prototype.GetMeasuredHeight(self)
	if ("fit" == height) then
		--log:Debug("cal fit height for text [%s] width [%s]", self.text:GetText(), width)
		if (not width) then
			error("width is needed to calculate Texts with \"fit\" height")
		end
		local oldWidth = self.text:GetWidth()
		self.text:SetWidth(width)
		local rtn = self.text:GetHeight()
		self.text:SetWidth(oldWidth)
		return rtn
	end
	return height
end

function yaka.Text.prototype:GetMeasuredWidth()
	local width = yaka.View.prototype.GetMeasuredWidth(self)
	if ("fit" == width) then
		height = self:GetMeasuredHeight()
		if (not height or ("string" == type(height))) then
			error("fix height needed for \"fit\" width of Text")
		end
		return self.text:GetStringWidth()
	end
	return width
end

function yaka.Text.prototype:Layout(x, y, width, height)
	--log:Debug("Layout x [%s] y [%s] width [%s] height [%s] cfgHeight [%s]", x, y, width, height, self.cfg.height)
	if ("fit" ~= self.cfg.height) then
		yaka.View.prototype.Layout(self, x, y, width, height)
	else
    	self.frame:ClearAllPoints()
    	self.frame:SetPoint("TOPLEFT", x, y)
    	self.frame:SetWidth(width)
    	self.text:SetWidth(width)
		self.frame:SetHeight(self.text:GetHeight())
	end
end

-- register view

YakaGui:RegisterViewType(yaka.Text)